/**
 * Created by Ren on 2015/5/21.
 */
function Director()
{
    this.ctx = null;    // canvas渲染上下文
    this.player = null; // 玩家
    this.enimes = [];   // 敌人集合
    this.boss = [];
    this.back = null;   // 背景
    this.playerBullets = [];  // 玩家子弹集合
    this.enimeyBullets = [];  // 敌人子弹集合
    this.backAudio = null;  // 背景音乐
    this.fireAudio = null;  // 发射子弹音乐
    this.bulletExplodeAudio = null // 子弹击中音乐
    this.enimyExplodeAudio = null;  // 敌人爆炸音乐
    this.playerExplodeAudio = null; // 玩家爆炸音乐
    this.starArray = [];
    this.FPS = 60;
    this.status = 1;
    this.actionID = null;
    this.animID = null;     // 刷帧ID
}
// 游戏方法
// 开始游戏
Director.prototype.play = function() {
    var temp = this;
    if(this.animID == null) {
        this.animID = window.setInterval(function () {
            temp.gameLoop();
        }, 1000 / this.FPS);
    }
    if(this.actionID == null)
    {
        this.actionID = window.setInterval(function(){
            temp.actionLoop();
        }, 10);
    }
    this.status = 1;
    if(this.backAudio)
    {
        this.backAudio.play();
    }
}
// 暂停游戏
Director.prototype.pause = function() {
    clearInterval(this.actionID);
    clearInterval(this.animID);
    this.actionID = null;
    this.animID = null;
    this.backAudio.pause();
    this.status = 2;
}
// 刷帧
Director.prototype.gameLoop = function() {
    // 清屏
    this.ctx.clearRect(0, 0, 600, 450);
    // 画背景
    this.back.draw();
    for (var i = 0; i < this.starArray.length; i++) {
        this.starArray[i].draw();
    }
    if (gameStatus.game_Status == 1) {
        // 画玩家
        this.player.draw();
        // 画敌人
        for (var i = 0; i < this.enimes.length; i++) {
            this.enimes[i].draw();
        }
        for (var i = 0; i < this.boss.length; i++) {
            this.boss[i].draw();
        }
        // 画子弹
        for (var i = 0; i < this.playerBullets.length; i++) {
            this.playerBullets[i].draw();
        }
        for (var i = 0; i < this.enimeyBullets.length; i++) {
            this.enimeyBullets[i].draw();
        }
        // 增加敌人
         temp = this;
         if(temp.boss.length < 1)
         {
             var addStatu = true;
             var ene = new Enemy(temp.ctx, temp.enimyExplodeAudio, parseInt(Math.random() * 500), -parseInt(Math.random() * 3 + 77),
                 temp.player, parseInt(Math.random() * 3), temp.enimeyBullets, temp.enimes, temp.bulletExplodeAudio, 1);
             ene.enemyLife = getWeight(ene.dx, ene.dy);
             for (var j = 0; j < temp.enimes.length; j++) {
                 if(isCollided(ene, temp.enimes[j]))
                 {
                     addStatu = false;
                     break;
                 }
             }
             if(addStatu&&Math.random() < 0.5)
             {
                 temp.enimes.push(ene);
             }
         }
        if(gameStatus.game_isBoss)
        {
            if(this.boss.length < 1)
            {
                var img = 'images/ene/'+(parseInt(Math.random()*13)+1)+'.png';
                var boss = new Boss(this.ctx, 250, -128, this.player, this.enimeyBullets, img, this.bulletExplodeAudio, this.boss);
                if(gameStatus.game_bossFireSpeed > 30)
                {
                    gameStatus.game_bossFireSpeed -= 10;
                }
                console.log(gameStatus.game_bossFireSpeed);
                this.boss.push(boss);
                gameStatus.game_isBoss = false;
            }
        }
    }
}
Director.prototype.actionLoop = function() {
    // 碰撞检测
    for (var i = 0; i < this.enimes.length; i++) {
        for (var j = 0; j < this.playerBullets.length; j++) {
            if (!this.enimes[i].exploded && !this.playerBullets[j].exploded) {
                if (isCollided(this.enimes[i], this.playerBullets[j])) {
                    this.playerBullets[j].exploded = true;
                    if (this.enimes[i].enemyLife < 0) {
                        this.player.score += parseInt(getWeight(this.enimes[i].dx, this.enimes[i].dy) * 20);
                        this.enimes[i].exploded = true;
                    }
                    else {
                        this.enimes[i].enemyLife -= 5;
                        this.enimes[i].isShow = true;
                    }
                }
            }
        }
    }
    for (var i = 0; i < this.boss.length; i++) {
        for (var j = 0; j < this.playerBullets.length; j++) {
            if (!this.boss[i].exploded && !this.playerBullets[j].exploded) {
                if (isCollided(this.boss[i], this.playerBullets[j])) {
                    if(isBossCollided(this.boss[i], this.playerBullets[j]))
                    {
                        /*                            console.log("okoko");*/
                        this.playerBullets[j].exploded = true;
                        if (this.boss[i].life < 1) {
                            this.boss[i].exploded = true;
                            this.player.score += 30;
                        }
                        else {
                            this.boss[i].isShow = true;
                            this.boss[i].life -= 5;
                        }
                    }
                    else
                    {
                        this.playerBullets[j].exploded = true;
                        this.boss[i].isShow = true;
                    }
                }
            }
        }
    }
    for (var i = 0; i < this.enimeyBullets.length; i++) {
        if (!this.enimeyBullets[i].exploded && !this.player.exploded) {
            if (isCollided(this.enimeyBullets[i], this.player)) {
                if (this.player.playerLife < 0) {
                    this.player.exploded = true;
                }
                else
                {
                    this.player.isShow = true;
                    this.player.playerLife -= 5;
                }
                this.enimeyBullets[i].exploded = true;
            }
        }
    }
    for (var i = 0; i < this.enimes.length; i++) {
        if (!this.player.exploded && !this.enimes[i].exploded && isCollided(this.enimes[i], this.player)) {
            if (this.player.playerLife < 1) {
                this.player.exploded = true;
            }
            else {
                this.isShow = true;
                this.player.playerLife -= getWeight(this.enimes[i].dx, this.enimes[i].dy);
            }
            this.player.score += parseInt(getWeight(this.enimes[i].dx, this.enimes[i].dy) * 20);
            this.enimes[i].exploded = true;
        }
    }
    for (var i = 0; i < this.enimeyBullets.length; i++) {
        for (var j = 0; j < this.playerBullets.length; j++) {
            if (this.enimeyBullets[i] && this.playerBullets[j] && isCollided(this.enimeyBullets[i], this.playerBullets[j])) {
                this.playerBullets[j].exploded = true;
                this.enimeyBullets[i].exploded = true;
                this.player.score += 1;
            }
        }
    }
    for (var i = 0; i < this.boss.length; i++) {
        if (!this.player.exploded && !this.boss[i].exploded && isCollided(this.boss[i], this.player)) {
            if(isBossCollided(this.boss[i], this.player))
            {
                if (this.player.playerLife < 1) {
                    this.player.exploded = true;
                }
                else {
                    this.isShow = true;
                    this.player.playerLife -= 5;
                }
                if (this.boss[i].life < 1) {
                    this.boss[i].exploded = true;
                    this.player.score += 30;
                }
                else {
                    this.isShow = true;
                    this.boss[i].life -= 5;
                }
            }
            else
            {
                if (this.player.playerLife < 1) {
                    this.player.exploded = true;
                }
                else {
                    this.isShow = true;
                    this.player.playerLife -= 5;
                }
            }
        }
    }
    for (var i = 0; i < this.starArray.length; i++) {
        var tmp = this.back.starCheck(this.starArray[i]);
        if (tmp) {
            this.starArray.splice(i, 1);
            i--;
        }
    }
    ;
    for (var i = 0; i < this.playerBullets.length; i++) {
        var tmp = this.back.bulletCheck(this.playerBullets[i]);
        if (tmp) {
            this.playerBullets.splice(i, 1);
            i--;
        }
    }
    ;
    for (var i = 0; i < this.enimeyBullets.length; i++) {
        var tmp = this.back.bulletCheck(this.enimeyBullets[i]);
        if (tmp) {
            this.enimeyBullets.splice(i, 1);
            i--;
        }
    }
    ;
    for (var i = 0; i < this.enimes.length; i++) {
        var tmp = this.back.enemyCheck(this.enimes[i]);
        if (tmp) {
            this.enimes.splice(i, 1);
            i--;
        }
    }
    for (var i = 0; i < this.enimes.length - 1; i++) {
        for (var j = i + 1; j < this.enimes.length; j++) {
            if (isEnemyCollided(this.enimes[i], this.enimes[j])) {
                var v1 = this.enimes[i].speed;
                var v2 = this.enimes[j].speed;
                var gg = this.enimes[i].direction;
                this.enimes[i].direction = this.enimes[j].direction;
                this.enimes[j].direction = gg;
                if(v1 > v2)
                {
                    this.enimes[i].speed = v2;
                    this.enimes[j].speed = v1;
                }
            }
        }
    }
}